
TARGET=task_finsh

BUILD_DIR=output

##### gcc
CROSS_COMPILE ?=arm-none-eabi-

CC=$(CROSS_COMPILE)gcc
AS=$(CROSS_COMPILE)gcc -x assembler-with-cpp
CP=$(CROSS_COMPILE)objcopy
SZ=$(CROSS_COMPILE)size
LD=$(CROSS_COMPILE)ld
DP=$(CROSS_COMPILE)objdump

DRIVER_DIR=../../drivers
SYS_DIR=../../drivers/sys
RTT_RTOS_DIR=../../../RTOS/rt_thread
PRINTF_DIR=../../../RTOS/printf

#### src files
SRC_C+=$(wildcard $(DRIVER_DIR)/*.c )
SRC_C+=$(wildcard $(SYS_DIR)/*.c )
SRC_C+=$(wildcard srcs/*.c)
SRC_C+=$(wildcard $(PRINTF_DIR)/*.c ) 

#rt-thread dir
SRC_C+=$(wildcard $(RTT_RTOS_DIR)/rt_thread_src/*.c)
SRC_C+=$(wildcard $(RTT_RTOS_DIR)/rt_thread_src/klibc/*.c)

SRC_C+=$(wildcard ../port/*.c)
SRC_C+=$(wildcard ../port/common/*.c)
# rt-thread components
SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/finsh/*.c) 
SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/libc/compilers/common/*.c) 
SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/libc/compilers/newlib/*.c) 
SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/drivers/core/*.c)
# SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/drivers/ipc/*.c)
# SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/drivers/pin/*.c)
# SRC_C+=$(wildcard $(RTT_RTOS_DIR)/components/drivers/serial/*.c)

SRC_S+=startup/startup_stm32f103xb.S startup/context_gcc.S
SRC_S+=$(wildcard ../port/common/*.S)

SRC_LD=link.ld

#### .o files
OBJ_C=$(addprefix $(BUILD_DIR)/,$(notdir $(SRC_C:.c=.o)))
OBJ_S=$(addprefix $(BUILD_DIR)/,$(notdir $(SRC_S:.S=.o)))
#### include dir
INC=-Iinc \
-I$(DRIVER_DIR)/include \
-I$(SYS_DIR) \
-I../port \
-I$(RTT_RTOS_DIR)/include \
-I$(PRINTF_DIR) \
-I../../../RTOS

# rt-thread components-I$(RTT_RTOS_DIR)/components/common/include  
INC+= \
-I$(RTT_RTOS_DIR)/components/finsh \
-I$(RTT_RTOS_DIR)/components/drivers/include \
# LIBC
INC+= \
-I$(RTT_RTOS_DIR)/components/libc/compilers/common/include \
-I$(RTT_RTOS_DIR)/components/libc/newlib \
-I$(RTT_RTOS_DIR)/components/libc/posix/io/epoll \
-I$(RTT_RTOS_DIR)/components/libc/posix/io/eventfd \
-I$(RTT_RTOS_DIR)/components/libc/posix/io/poll \
-I$(RTT_RTOS_DIR)/components/libc/posix/ipc \




#### CFLAGS-D_POSIX_C_SOURCE=1 
DEFS=-DUSE_HAL_DRIVER -DSTM32F103xB  \
-DRT_USING_DLIBC \
-DRT_USING_LIBC \
-D__RTTHREAD__ \
-D__RT_KERNEL_SOURCE__ \
-D_DLIB_ADD_EXTRA_SYMBOLS=0 \
-DPRINTF_INCLUDE_CONFIG_H


 
CFLAGS= \
-mcpu=cortex-m3 \
-mthumb \
$(DEFS) \
-Wall \
-fdata-sections \
-ffunction-sections \
-Og \
#-Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst))
#-g \
#-gdwarf-2 \
#-MMD -MP -MF"$(@:%.o=%.d)"


#### LINK 
LIBS=-lc -lm #-lnosys

LDFLAGS =-mcpu=cortex-m3 \
-Wl,--gc-sections \
-specs=nano.specs -T$(SRC_LD) \
$(LIBS) \
-Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref


### .c path set
vpath %.c $(sort $(dir $(SRC_C) ))
vpath %.S $(sort $(dir $(SRC_S) ))
 
#### build
.PHONY:all clean  test dis d

all: $(BUILD_DIR) $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).bin  $(BUILD_DIR)/$(TARGET).dis
	@echo all

# mkdir 
$(BUILD_DIR):
	mkdir $@

# link elf
$(BUILD_DIR)/$(TARGET).elf:$(OBJ_C) $(OBJ_S)
	@echo  abc
	$(CC) $(OBJ_C) $(OBJ_S) $(LDFLAGS) -o $@
	$(SZ) $@

# .c -> .o
$(BUILD_DIR)/%.o : %.c
	@echo CC $<
	@$(CC) -c $(CFLAGS)  $< $(INC) -o $@
	

# .S -> .o
$(BUILD_DIR)/%.o : %.S
	@echo AS $<
	$(AS) -c $(CFLAGS) $< $(INC) -o $@

# .elf -> .bin
$(BUILD_DIR)/$(TARGET).bin:$(BUILD_DIR)/$(TARGET).elf
	@$(CP) -O binary -S $< $@

#.elf -> dis
$(BUILD_DIR)/$(TARGET).dis:$(BUILD_DIR)/$(TARGET).elf
	@$(DP) -DS $< > $@
clean:
	rm -rf $(BUILD_DIR)

dis:$(BUILD_DIR)/$(TARGET).elf
	$(DP) -DSh $< > $(BUILD_DIR)/$(TARGET).dis

test:
#	@echo $(SRC_C) 
	@echo $(OBJ_C)  

c:
	make clean

d:
	
	make all -j12

#-include $(wildcard $(BUILD_DIR)/*.d)